package com.znz.analysis.hdfs;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.znz.analysis.constant.enumeration.HDFSFileType;
import com.znz.analysis.constant.properties.HDFSProperties;

@Component
public class HDFSFile {

	private static final Logger logger = Logger.getLogger(HDFSFile.class.getName());

	@Resource
	private HDFSProperties hdfsProperties;
	@Resource
	private WebHdfsHandler webHdfsHandler;

	/**
	 * 从HDFS获取文件路径
	 * 
	 * @param domains
	 * 
	 * @return
	 */
	public List<String> getPaths(String dir, List<String> domains, HDFSFileType type) {
		logger.setLevel(Level.INFO);
		logger.info("domains: " + domains);
		List<String> list = new ArrayList<String>();
		List<String> paths = webHdfsHandler.getFiles(dir, type);
		// 从配置文件中读取的HDFS host
		String host = hdfsProperties.getHost();
		paths.forEach(i -> {
			// 筛选".log"和".gz"后缀的文件
			if (HDFSFileType.FILE == type) {
				// 文件类型筛选
				if (i.endsWith(".log") || i.endsWith(".gz")) {
					// 根据API返回的域名筛选过滤
					// List<String> arr = domains;
					// if (domains.size() == 0) {
					// arr = Arrays.asList("www.sztdcoin.com",
					// "api.sztdcoin.com", "aedutest.test.com", ".mobo360.com",
					// "www.mobaopay.com", "saasmerchant.mobaopay.com",
					// "saasadmin.mobaopay.com",
					// "saascashier.mobaopay.com", "pay.mobaopay.com",
					// "p2ppay.mobaopay.com",
					// "p2p.mobaopay.com", "mobaopay.com",
					// "cbpaymerchant.mobaopay.com",
					// "cbpaycashier.mobaopay.com", "cbpayadmin.mobaopay.com",
					// ".mobaopay.com");
					// }
					for (String domain : domains) {
						if (i.contains(domain)) {
							list.add("hdfs://" + host + ":9000" + dir + "/" + i);
							break;
						}
					}
				}
				// DIRECTORY类型直接加入
			} else {
				list.add(i);
			}
		});
		return list;
	}

	/**
	 * 计算昨天的日期时间戳,得到目录名称
	 * 
	 * @return
	 */
	private String getYesterdayDirName(SimpleDateFormat sdf) {
		// 计算昨天的日期时间戳,得到目录名称
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
		String timespan = sdf.format(calendar.getTime());
		return timespan;
	}

	/**
	 * 从HDFS获取昨天日志文件路径 例如今天日期20170628对应HDFS目录logs/20170628中的时间戳20170626-
	 * 
	 * @param domains
	 * 
	 * @return
	 */
	public List<String> getYesterdayFiles(String dir, List<String> domains) {
		List<String> files = new ArrayList<String>();
		// 计算昨天的日期时间戳,得到目录名称
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String yesterdayDirName = getYesterdayDirName(sdf);
		// 获得指定目录下的所有子目录名称
		List<String> subDirNames = getPaths(dir, domains, HDFSFileType.DIRECTORY);
		if (subDirNames.contains(yesterdayDirName)) {
			files = getPaths(dir + "/" + yesterdayDirName, domains, HDFSFileType.FILE);
		}
		return files;
	}

}
